package com.cuitrip.business.notice;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.app.base.BaseVerticalListActivity;
import com.cuitrip.app.base.PaddingItemDecoration;
import com.cuitrip.business.notice.ui.IMessageListView;
import com.cuitrip.business.notice.ui.MessageMode;
import com.cuitrip.business.notice.ui.MessagePresent;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.component.list.simple.CTSimpleViewHolder;
import com.lab.component.list.swipe.SwipeLayout;
import com.lab.utils.LogHelper;
import com.lab.utils.Utils;

import java.util.List;

import jp.wasabeef.recyclerview.animators.BaseItemAnimator;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;

/**
 * Created by baziii on 15/8/10.
 */
public class MessageListActivity extends BaseVerticalListActivity<MessageMode> implements IMessageListView {
    public static final String TAG = "MessageListActivity";
    MessagePresent mMessagePresent = new MessagePresent(this);

    @Override
    public void initView() {
        super.initView();
        BaseItemAnimator animator = new SlideInUpAnimator();
        mRecyclerView.setItemAnimator(animator);
        mRecyclerView.addItemDecoration(new PaddingItemDecoration(0, 0, Utils.dp2pixel(1), 0));
        mRecyclerView.setEmptyText(ResourceUtil.getString(R.string.message_feedback_no_result));
        setupMoreLeftCount(4);
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.message_item;
    }

    @Override
    public void bindData(CTSimpleViewHolder holder, final int position, final MessageMode item) {
        holder.getView(R.id.ct_message_layout).setTag(position);
        holder.getView(R.id.ct_delete).setTag(item);

        TextView mMessageTime = holder.getView(R.id.ct_message_time);
        mMessageTime.setText(
                TimeUtil.formatTime(
                        InputTime.build(item.getGmtCreated()),
                        OutputTime.getLocalInstance(OutputTime.OutputType.Type_Minute)
                )
        );

        TextView contentView = holder.getView(R.id.message_content);
        contentView.setText(item.getContent());

        TextView messageStatusView = holder.getView(R.id.message_status);


        if (item.isRead()) {
            messageStatusView.setTextColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
            contentView.setTextColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        } else {
            messageStatusView.setTextColor(ResourceUtil.getThemeColor());
            contentView.setTextColor(ResourceUtil.getColor(R.color.qiaomai_7c706e));
        }
        TextView mMessageName = holder.getView(R.id.ct_message_name);
        if (item.getType() == MessageMode.SYSTEM_TYPE) {
            messageStatusView.setText(ResourceUtil.getString(R.string.setting_full_icon));
            mMessageName.setText(ResourceUtil.getString(R.string.message_type_system));
        } else {
            messageStatusView.setText(ResourceUtil.getString(R.string.order_icon));
            mMessageName.setText(ResourceUtil.getString(R.string.message_type_book));
        }

        holder.getView(R.id.ct_message_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMessagePresent.onClickMessage(item);
                item.setIsRead(true);
                getAdapter().notifyItemChanged(position);
            }
        });

        holder.getView(R.id.ct_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMessagePresent.onMove(item);
            }
        });
    }

    @Override
    public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType) {
        SwipeLayout swipeLayout = viewHolder.getView(R.id.ct_swip_layout);
        swipeLayout.setDragEdge(SwipeLayout.DragEdge.Right);
        swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
    }

    @Override
    public void initData() {
        super.initData();
        showProgressView();
        requestPresentRefresh();
    }

    @Override
    public void jumpMessage(MessageMode messageMode) {
        LogHelper.e(TAG, "jumpMessage :" + messageMode.toString());
//        BrowserActivity.startWithData(this,messageMode.content);
        MessageDetailActivity.start(this, messageMode);
    }


    @Override
    public int getSize() {
        return getAdapter().getItemCount();
    }

    @Override
    public void deleteMessage(MessageMode messageMode) {
        getAdapter().removeMessageMode(messageMode);
    }

    @Override
    public void renderUIWithData(List<MessageMode> items) {
        getAdapter().setDataList(items);
    }

    @Override
    public void renderUIWithAppendData(List<MessageMode> items) {
        getAdapter().appendDataList(items);
    }

    @Override
    public void requestPresentLoadMore() {
        showLoadingMoreView();
        mMessagePresent.requestLoadMore();
    }

    @Override
    public void requestPresentRefresh() {
        hideLoadingMoreView();
        mMessagePresent.requestRefresh();
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarMiddleAreaName = getString(R.string.message);
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        return config;
    }
}
